toNegativeIntOrThrow
Returns this number as a NegativeInt, which may involve rounding or truncation, or throws IllegalArgumentException if this number is strictly positive.
var result: NegativeInt = (-1).toNegativeIntOrThrow()
println(result) // -1
result = 0.toNegativeIntOrThrow()
println(result) // 0
1.toNegativeIntOrThrow() // IllegalArgumentException
Content copied to clipboard
You can use the toNegativeIntOrNull function for returning null
instead of throwing an IllegalArgumentException when this number is strictly positive.